package org.example.demo;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class Application {
    public void run(String[] args) {
        JFrame frame = FrameFractory.createJFrameDefault();
        Container contentPane = frame.getContentPane();
        frame.setBounds(500, 100, 900, 700);
        frame.setResizable(false);

        SnakePanel snakePanel = new SnakePanel();
        contentPane.add(snakePanel);

        frame.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                switch (e.getKeyCode()) {
                    case KeyEvent.VK_SPACE:
                        Resource.STT.toggle();
                        snakePanel.repaint();
                        break;
                    case KeyEvent.VK_UP:
                        Resource.SNAKE.changeDir(Direction.U);
                        break;
                    case KeyEvent.VK_DOWN:
                        Resource.SNAKE.changeDir(Direction.D);
                        break;
                    case KeyEvent.VK_LEFT:
                        Resource.SNAKE.changeDir(Direction.L);
                        break;
                    case KeyEvent.VK_RIGHT:
                        Resource.SNAKE.changeDir(Direction.R);
                        break;
                    default:
                        break;
                }

            }
        });

        new Timer(100, e -> {
            if (Resource.STT.getRunning() && !Resource.STT.getGameOver()) {
                try {
                    Resource.SNAKE.go();
                } catch (GameOverException ex) {
                    Resource.STT.gameOver();
                }
                snakePanel.repaint();
            }
        }).start();


    }
}
